﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace eStore
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        public string demo;
        private eStoreModelDataContext db;
        protected void Page_Load(object sender, EventArgs e)
        {
            db = new eStoreModelDataContext();
            string lang = ConfigurationManager.AppSettings["DefaultLanguage"];
            if (Session["lang"]!=null)
            {
                lang = Session["lang"].ToString();
            }
            Page_LoadMenu(lang);
            Page_LoadLanguage(lang);
            LabelWelcomeUserName.Text = String.Empty;
            if (Request.IsAuthenticated)
            {
                LabelWelcomeUserName.Text = (string)GetGlobalResourceObject("eStore", "LabelWelcomeUserName")+" "+Page.User.Identity.Name;
                LabelWelcomeUserName.NavigateUrl = "~/User/UserInfo.aspx";
            }
            else
            {
                LabelWelcomeUserName.Text = String.Empty;
            }
        }

        private void Page_LoadMenu(string lang)
        {
            List<eStore_Menu> ListMenu = new List<eStore_Menu>();
            if (Request.IsAuthenticated)
            {
                ListMenu = db.eStore_Menus.Where(d => d.MenuType == "MenuTop" && d.Role == "*" && d.State == true && d.MenuNameEN != "Register" && d.MenuNameEN != "Login").OrderBy(d => d.MenuID).ToList(); 
            }
            else
            {
                ListMenu = db.eStore_Menus.Where(d => d.MenuType == "MenuTop" && d.Role == "*" && d.State == true && d.MenuNameEN != "LogOut").OrderBy(d => d.MenuID).ToList();
            }
           
            int iMenu = ListMenu.Count;
            if (iMenu > 0)
            {
                for (int i = 0; i < iMenu; i++)
                {
                    MenuItem item = new MenuItem();
                    if (lang == "vi")
                    {
                        item.Text = ListMenu[i].MenuNameVN; ;
                    }
                    if (lang == "en")
                    {
                        item.Text = ListMenu[i].MenuNameEN; ;
                    }
                    item.NavigateUrl = ListMenu[i].Link;
                    MenuTop.Items.Add(item);
                }
            }
        }
        private void Page_LoadLanguage(string lang)
        {
            List<eStore_Language> ListLanguage = new List<eStore_Language>();
            ListLanguage = db.eStore_Languages.ToList();
            int iLanguage = ListLanguage.Count;
            if (iLanguage > 0)
            {
                VietnameseButton.ImageUrl = ListLanguage[1].Image;
                VietnameseButton.ToolTip = ListLanguage[1].LangName;
                EnglishButton.ImageUrl = ListLanguage[0].Image;
                EnglishButton.ToolTip = ListLanguage[0].LangName;

            }
        }
        protected void VietnameseButton_Click(object sender, ImageClickEventArgs e)
        {
            Session["lang"] = "vi";
            Response.Redirect(Request.Url.ToString());
        }

        protected void EnglishButton_Click(object sender, ImageClickEventArgs e)
        {
            Session["lang"] = "en";
            Response.Redirect(Request.Url.ToString());
        }
    }
}
